#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<vector<int>> cnt(n);
    for (int i = 0; i < n; i++)
    {
        cnt[i].push_back(a[i]);
        int x = a[i] ^ (a[i] >> 1);
        while (x != a[i])
        {
            cnt[i].push_back(x);
            x = x ^ (x >> 1);
        }
    }
    int ans = 0;
    for (int i = 0, j = n - 1; i < j; i++, j--)
    {
        int x = inf;
        for (int k = 0; k < cnt[i].size(); k++)
        {
            auto it = find(cnt[j].begin(), cnt[j].end(), cnt[i][k]);
            if (it == cnt[j].end()) continue;
            int index = it - cnt[j].begin();
            x = min(x, k + index);
        }
        if (x == inf)
        {
            cout << -1 << endl;
            return;
        }
        ans += x;
    }
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}